package com.nobuyme.box.toy;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.nobuyme.box.R;
import com.nobuyme.box.account.Account;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.app.UrlCenter;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;

import java.io.UnsupportedEncodingException;

import butterknife.Bind;
import butterknife.OnClick;

public class ToyReturnActivity extends BaseActivity {
    String result_url;
    @Bind(R.id.iv_score)
    ImageView mIvScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
        hideToolbar();
        checkNetWork();

    }

    private void checkNetWork() {
        HttpRequest request = new HttpRequest(this);
        request.get(UrlCenter.AGE_LIST, null, new DataRequestListener<Object>(Object.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                initScore();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);

            }
        });

    }


    //设置二维码
    private void initScore() {
        Bitmap mBitmap = null;

        Account acc = AccountManager.getCurrentAccount();
        int id = acc.getId();
        String str = String.format(UrlCenter.RETUNT_GOODS,id);
        try {
            String s = new String(Base64.encode(str.getBytes("utf-8"), Base64.NO_WRAP),"utf-8");
            result_url = String.format(UrlCenter.SCAN_ADRESS, s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(TextUtils.isEmpty(result_url)){
            Toast.makeText(ToyReturnActivity.this, "您的输入为空!", Toast.LENGTH_SHORT).show();
            return;
        }else{
            Glide.with(ToyReturnActivity.this).load(result_url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache( true ).into(mIvScore);
        }
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, ToyReturnActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_toy_return, null);

    }

    @OnClick(R.id.btn_return_back)
    public void onViewClicked() {
        finish();
    }
}
